import javax.swing.*;
import javax.swing.table.DefaultTableModel;

import java.awt.event.*;
import java.sql.SQLException;

/**
 * ViewAppointments
 * Shows a display of all appointments for the selected time period
 *
 *
 *  Date           	Name            	Action
 *  --------------------------------------------------------------------------------------
 *	03/06/11		Bethany				Created Class
 *
 *
 *
 * @author bethanysumner
 * @version $Revision: 1.0 $
 */


public class ViewAppointments extends JPanel{
	
	/**
	 * Field main.
	 */
	private MainWindow main;
	/**
	 * Field database.
	 */
	private Database database;
	
	/**
	 * Field btnClose.
	 */
	/**
	 * Field btnEditTreatment.
	 */
	/**
	 * Field btnCreateAppt.
	 */
	/**
	 * Field btnDeleteAppt.
	 */
	private JButton btnCreateAppt, btnEditTreatment, btnClose, btnDeleteAppt;
	/**
	 * Field table.
	 */
	private JTable table;
	/**
	 * Field model.
	 */
	private DefaultTableModel model;
	//private TableListener tableListener;
	
	/**
	 * ViewAppointment constructor
	 * @param main MainWindow
	 * @param database Database
	 */
	public ViewAppointments(MainWindow main, Database database) {
		
		this.main = main;
		this.database = database;
		
		ButtonListener buttonListener = new ButtonListener();
		createLayout();
		
		switch(main.getUsertype()){
		
		case 3: //patient -- views appointments that he/she has, edit, treatment record, make new appointment, view invoice
			
			btnCreateAppt = new JButton("Request New Appt");
			btnCreateAppt.setBounds(401, 390, 188, 29);
			btnCreateAppt.addActionListener(buttonListener);
			add(btnCreateAppt);
			
			showPatientsAppts();
			
			break;
			
		default: // nurse, doctor, and admin can edit treatment records
			
			btnEditTreatment = new JButton("Treatment Record");
			btnEditTreatment.setBounds(20, 390, 188, 29);
			btnEditTreatment.addActionListener(buttonListener);
			add(btnEditTreatment);
			
			showAllAppts();
			
			break;
				
		}
		
		
	} //end ViewUsers()
	
	
	/**
	 * Create the layout for the GUI elements
	 */
	private void createLayout(){
		
		setLayout(null);
		
		// BUTTONS
		ButtonListener buttonListener = new ButtonListener();
		btnClose = new JButton("Close");
		btnClose.setBounds(585, 390, 182, 29);
		btnClose.addActionListener(buttonListener);
		add(btnClose);
		
		btnDeleteAppt = new JButton("Delete Appointment");
		btnDeleteAppt.setBounds(222, 390, 182, 29);
		btnDeleteAppt.addActionListener(buttonListener);
		add(btnDeleteAppt);
		
	}//end createLayout
	
	/**
	 * Shows all appointments in the system
	 */
	private void showAllAppts(){
		String[] columnNames = {"Date", "Time", "PatientID", "Reason", "Doctor"};
		String[][] data;
		try {
			data = database.getAllAppts();
			addTable(data, columnNames);
		} catch (SQLException e) {
			main.showError("Error Connecting with Database");
			e.printStackTrace();
		}
	}
	
	/**
	 * Shows all appointments for a given patient
	 */
	private void showPatientsAppts(){
		String[] columnNames = {"Date", "Time", "Reason", "Doctor"};
		String[][] data;
		try {
			data = database.getPatientAppts(main.getCurrentUser());
			addTable(data, columnNames);
		} catch (SQLException e) {
			main.showError("You have no appointments.");
			e.printStackTrace();
		}
	}
	
	/**
	 * Creates the table with the given data and columnNames
	 * @param data Data to populate the table
	 * @param columnNames Column Names
	 */
	private void addTable(String[][] data, String[] columnNames){
		
		model = new DefaultTableModel(data, columnNames);
		table = new JTable(model);
		
		JScrollPane scrollPane = new JScrollPane(table);
		scrollPane.setBounds(20, 60, 753, 318);
		add(scrollPane);
		
	}
	
	/**
	 * Brings up panel to create new appointment
	 */
	private void createAppt(){
		main.addTab("Request Appointment", new AddEditAppointment(main, database));
	}
	
	/**
	 * Brings up the AddEditTreatmentRecord panel to Add/Edit a treatment record
	 */
	private void editTreatment(){
			
		if(table.getSelectedRow() >= 0){
			String date = table.getValueAt(table.getSelectedRow(), 0).toString();
			String time = table.getValueAt(table.getSelectedRow(), 1).toString();
			main.addTab("Treatment Record", new AddEditTreatmentRecord(main, database, date, time));
		}else{
			main.showError("Please select an appointment for the treatment record.");
		}
	}
	
	/**
	 * Closes the tab
	 */
	private void close(){
		main.removeTab(this);
		main.clearMessage();
	}
	
	
	/**
	 * Deletes the appointment selected
	 */
	private void deleteAppt(){
		if(table.getSelectedRow() > 0){
			final String selectedDate = table.getValueAt(table.getSelectedRow(), 0).toString();
			final String selectedTime = table.getValueAt(table.getSelectedRow(), 1).toString();
			try {
				database.deleteAppt(selectedDate, selectedTime);
				model.removeRow(table.getSelectedRow());
				main.showAffirmation("The appointment has been deleted");
			} catch (SQLException e) {
				main.showError("Error deleting Appointment");
				e.printStackTrace();
			}
		}else{
			main.showError("Please select an appointment to delete");
		}
	}
	
	
	
	
	
	
	/**
	 * ButtonListener
	 * ActionListener to listen when button has been pressed
	 *
	 *
	 *  Date           	Name            	Action
	 *  --------------------------------------------------------------------------------------
	 *	03/06/11		Bethany				Created Class
	 *
	 *
	 *
	 * @author bethanysumner
	 * @version 1.0
	 */
	private class ButtonListener implements ActionListener{
		
		/**
		 * Performs tasks corresponding to the action
		 * @param event The action event
		 * @see java.awt.event.ActionListener#actionPerformed(ActionEvent)
		 */
		public void actionPerformed(ActionEvent event){
			
			if(event.getSource().equals(btnClose)){
				close();
			}else if(event.getSource().equals(btnCreateAppt)){
				createAppt();
			}else if(event.getSource().equals(btnEditTreatment)){
				editTreatment();
			}else if(event.getSource().equals(btnDeleteAppt)){
				deleteAppt();
			}
			
		}
		
	}// end ButtonListener
	
}
